package pers.feng1240.blog.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import pers.feng1240.blog.entities.Blog;
import pers.feng1240.blog.service.BlogService;
import pers.feng1240.blog.service.TagService;
import pers.feng1240.blog.service.ThemeService;
import pers.feng1240.blog.util.FileUpload;
import pers.feng1240.blog.util.PageHelper;

/*
 * 博客controller
 */

@Controller
public class BlogController {

	@Resource
	private BlogService blogService; 	//业务逻辑
	@Resource
	private ThemeService themeService;	//主题业务逻辑
	@Resource
	private TagService tagService;	//标签业务逻辑
	
	/*
	 * 分页列表
	 * @param pageIndex 要获取的页码
	 * @request map 中  objectList对象列表  pageIndex 要获取的页码  pageSize 页数
	 */
	@RequestMapping("admin/getBlogList")
	public String getBlogList(String pageIndex,HttpServletRequest request){
		request.setAttribute("map", blogService.getPageList(PageHelper.pageIndex(pageIndex)));
		request.setAttribute("action","getBlogList.do" ); //设置分页动态action
		return "forward:blogs.jsp";	
	}
	
	/*
	 * 一条详情
	 */
	@RequestMapping("admin/getBlogById")
	public String getBlogById(int id,HttpServletRequest request){
		request.setAttribute("blog", blogService.getById(id));
		return "forward:blog_detail.jsp";
	}
	
	
	/*
	 * 写博客  初始化 标题  标签
	 */
	@RequestMapping("admin/writing")
	public String writing(HttpServletRequest request){
		request.setAttribute("themeList", themeService.getObjectList());
		request.setAttribute("tagList", tagService.getObjectList());
		return "forward:writing.jsp";
	}
	
	/*
	 * 发布博客
	 * @param blog 博客对象
	 * @param tags 标签数组
	 */
	@RequestMapping("admin/addBlog")
	public String addBlog(@RequestParam(value = "file", required = false) MultipartFile file,Blog blog,int[]tags,HttpServletRequest request){
	    
		blog.setBlogPicture(FileUpload.uploadPic(file, request));
		//是否置顶
	    if(blog.getBlogTop()==1){
	        //置顶  先撤销原有置顶博客，再保存
	        blogService.unTop();
	        blogService.writing(blog, tags);
	    }else{
	        blogService.writing(blog, tags);
	    }
		return "redirect:getBlogList.do";
	}
	
	/*
	 * 更新、修改
	 */
	@RequestMapping("admin/updateBlog")
	public String updateBlog(Blog blog){
		blogService.updateObject(blog);
		return "redirect:getBlogList.do";
	}
	
	/*
	 * 冻结
	 * @param id 博客id
	 */
	@RequestMapping("admin/setFreeze")
	public String setFreeze(String type,int id,String pageIndex,HttpServletRequest request){   
        if("freeze".equals(type)){
            //冻结
            blogService.setFreeze(id);
        }else{
            blogService.setUnFreeze(id);
        }
        request.setAttribute("map", blogService.getPageList(PageHelper.pageIndex(pageIndex)));
        request.setAttribute("action","getBlogList.do" ); //设置分页动态action
        return "forward:blogs.jsp";
    }
	
	/*
	 * 置顶
	 * @param type 类型 置顶或取消置顶
	 * @param id 博客id
	 * @param pageIndex 当前页
	 */
	@RequestMapping("admin/setTop")
	public String setTop(String type,int id,String pageIndex,HttpServletRequest request){   
		if("top".equals(type)){
		    //置顶  先撤销原有置顶博客，再保存
            blogService.unTop();
            blogService.setTop(id);
		}else{
		    blogService.unTop();
		}
		request.setAttribute("map", blogService.getPageList(PageHelper.pageIndex(pageIndex)));
        request.setAttribute("action","getBlogList.do" ); //设置分页动态action
		return "forward:blogs.jsp";
	}
	
	/*
	 * 删除
	 */
	@RequestMapping("admin/deleteBlog")
	public String deleteOnject(int id){
		blogService.deleteById(id);
		return "redirect:getBlogList.do";
	}
	
}
